package com.meida.student.app.user.controller;

import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.bbs.client.entity.BbsContent;
import com.meida.module.bbs.provider.service.BbsContentService;
import com.meida.module.bus.client.entity.BusTestpaper;
import com.meida.module.file.client.entity.SysFile;
import com.meida.module.file.provider.service.SysFileService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 我的
 *
 * @author flyme
 * @date 2021-08-03
 */
@Slf4j
@RestController
@RequestMapping("/bbs/content/")
public class ApiBbsController extends BaseController<BbsContentService, BbsContent> {

    @Autowired
    private SysFileService sysFileService;

    @ApiOperation(value = "保存草稿箱", notes = "保存草稿箱")
    @GetMapping(value = "saveDraftBox")
    public ResultBody saveDraftBox(@RequestParam(required = false) Map params) {
        Long contentId = TypeUtils.castToLong(params.get("contentId"));
        BbsContent bbsContent = bizService.getById(contentId);
        Integer contentState = bbsContent.getContentState();
        //已发布状态
        if (contentState == 1) {
            CriteriaQuery cq = new CriteriaQuery(BbsContent.class);
            cq.eq("parentId", contentId);
            BbsContent braftBoxBbsContent = bizService.getOne(cq);
            if (FlymeUtils.isEmpty(braftBoxBbsContent)) {
                //备份帖子
                BbsContent bbsContent1 = new BbsContent();
                BeanUtils.copyProperties(bbsContent, bbsContent1);
                bbsContent1.setContentId(IdWorker.getId());
                bbsContent1.setParentId(contentId);
                bbsContent1.setContentState(-1);
                bizService.save(bbsContent1);
                //备份附件
                List<SysFile> sysFileList = selectFileList(contentId);
                Long contentId1 = bbsContent1.getContentId();
                contentId = contentId1;
                if (sysFileList.size() > 0) {
                    sysFileList.forEach(sysFile -> {
                        long id = IdWorker.getId();
                        log.info("备份原附件：新附件Id" + id);
                        sysFile.setFileId(id);
                        sysFile.setBusId(contentId1);
                        sysFileService.save(sysFile);
                    });
                }
            } else {
                contentId = braftBoxBbsContent.getContentId();
            }
        }
        return ResultBody.ok(contentId);
    }

    /**
     * 查询帖子附件
     *
     * @param contentId
     * @return
     */
    List<SysFile> selectFileList(Long contentId) {
        CriteriaQuery cq = new CriteriaQuery(BbsContent.class);
        cq.eq(true, "busId", contentId);
        cq.eq(true, "busType", BbsContent.class.getSimpleName());
        return sysFileService.list(cq);
    }

}
